package cn.edu.java.thread.原子类;

import lombok.SneakyThrows;

import java.util.concurrent.atomic.AtomicLong;

public class AtomicLongDemo {
    private static AtomicLong atomicLong = new AtomicLong(0);
    private static Long aLong = 0L;

    @SneakyThrows
    public static void main(String[] args) {
        // 创建多个线程，每个线程对计数器执行递增操作
        Thread[] threads = new Thread[100];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    atomicLong.incrementAndGet();
                    aLong += 1;
                }
            });
            threads[i].start();
        }

        for (int i = 0; i < threads.length; i++){
            threads[i].join();
        }


        // 输出最终的计数器值
        System.out.println("AtomicLong: " + atomicLong.get());
        System.out.println("Long: " + aLong);
    }
}

